<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
class Resume extends Controller{
    /****
     Class register           个人简历——个人信息查看
     *@user_id[int]           用户id
    */
     public function index(){
        $user_id = input('user_id');
        $personal = Db::name('personal')->where(['user_id'=>$user_id])->find();
        $strength = Db::name('strength')->where(['user_id'=>$user_id])->find();
        $expect = Db::name('expect')->where(['user_id'=>$user_id])->find();
        $occupational_history = Db::name('occupational_history')->where(['user_id'=>$user_id])->select();
        if($user_id != null){
          $arr = [
            'state'                 =>1,
            'personal'              =>$personal,
            'strength'              =>$strength,
            'expect'                =>$expect,
            'occupational_history'  =>$occupational_history,
            'message'               =>'请求成功'
          ];
        }else{
          $arr = [
            'state'=>0,
            'message'=>'请求失败'
          ];
        }
        return json($arr);
     }
    /****
     Class register           个人简历——个人信息查看
     *@user_id[int]           用户id
    */
    public function personal_index(){
      $user_id = input('user_id');
      $personal = Db::name('personal')
            ->where(['user_id'=>$user_id])
            ->find();
      if($personal){
        $arr = [
          'state'     =>1,
          'personal' =>$personal,
          'message'   =>'请求成功'
        ];
      }else{
        $arr = [
          'state'=>1,
          'personal' =>'',
          'message'=>'空'
        ];
      }
      return json($arr);
    }
    /****
     Class register           个人简历——个人信息编辑
     *@user_id[int]           用户id
     *@name                   姓名
     *@gender                 性别    
     *@age                    年龄
     *@image                  图像
     *@addtime                添加时间
     *@updatetime             更新时间
    */ 
    public function personal(){
        $post = input('post.');
        $personal = Db::name('personal')
            ->where(['user_id'=>$post['user_id']])
            ->find();
        if($personal){
          $data = [
            'user_id'	=>$post['user_id'],
            'name'		=>$post['name'],
            'gender'	=>$post['gender'],
            'age'		=>$post['age'],
			'degree'    =>$post['degree'],
            'image'		=>$post['image'],
            'updatetime'=>time(),
          ];
            $personal_update = Db::name('personal')->where(['user_id'=>$post['user_id']])->update($data);
            if($personal_update){
              $arr = [
                'state'=>1,
                'message'=>'请求成功'
               ];
            }else{
               $arr = [
                'state'=>0,
                'message'=>'请求失败'
               ];
            }
            return json($arr);
          }
          $data_insert = [
            'user_id'=>$post['user_id'],
            'name'=>$post['name'],
            'gender'=>$post['gender'],
            'age'=>$post['age'],
			'degree'    =>$post['degree'],
            'image'=>$post['image'],
            'addtime'=>time(),
          ];
         $personal_insert = Db::name('personal')->insert($data_insert);
          if($personal_insert){ 
            $arr = [
                'state'=>1,
                'message'=>'请求成功'
            ];
          }else{
              $arr = [
                'state'=>0,
                'message'=>'请求失败'
               ]; 
          }
          return json($arr);
    }

    /****
     Class register           个人简历——个人优势
     *@user_id[int]           用户id
    */
    public function strength(){
      $user_id = input('user_id');
      $strength = Db::name('strength')
                  ->where(['user_id'=>$user_id])
                  ->find();
      
      if($strength){
        $arr = [
              'state'     =>10,
              'strength'  =>$strength,
              'message'   =>'请求成功'
          ];
          return json($arr);
      }
    }

    /****
     Class register           个人简历——个人优势
     *@user_id[int]           用户id
     *@content[int]           内容
    */
    public function strength_update(){
        $post = input('post.');
        $strength = Db::name('strength')
                  ->where(['user_id'=>$post['user_id']])
                  ->find();
        if($strength){
          $data = [
            'content'=>$post['content'],
            'updatetime'  =>time()
          ];
          $strength_update = Db::name('strength')->where(['user_id'=>$post['user_id']])->update($data);
          if($strength_update){ 
          $arr = [
              'state'=>1,
              'message'=>'请求成功'
          ];
        }else{
            $arr = [
              'state'=>0,
              'message'=>'请求失败'
             ]; 
        }
        return json($arr);
        }
         $data_insert = [
            'user_id'   =>$post['user_id'],
            'content'   =>$post['content'],
            'addtime'   =>time()
          ];
          $strength_add = Db::name('strength')->insert($data_insert);
          if($strength_add){ 
          $arr = [
              'state'=>1,
              'message'=>'请求成功'
          ];
        }else{
            $arr = [
              'state'=>0,
              'message'=>'请求失败'
             ]; 
        }
        return json($arr);
      }

      /****
     Class register           个人简历——求职期望
     *@user_id[int]           用户id
    */
    public function expect(){
      $user_id = input('user_id');
      $expect = Db::name('expect')
                  ->where(['user_id'=>$user_id])
                  ->find();
      if($expect){
        $arr = [
              'state'     =>1,
              'expect'  =>$expect,
              'message'   =>'请求成功'
          ];
         
      }else{
        $arr = [
              'state'     =>1,
              'expect'    =>'',
              'message'   =>'空'
          ];
      }
       return json($arr);
    }
     /**** 
     Class register           个人简历——求职期望更新
     *@user_id[int]           用户id
     *@position               期望职位
     *@pay                    薪资要求
     *@nature                 工作性质
     *@province               省
     *@city                   市
     *@district               区
    */
    public function expect_update(){
      $post = input('post.');
      $expect = Db::name('expect')
                  ->where(['user_id'=>$post['user_id']])
                  ->find();
      if($expect){
          $data = [
            'position'    =>$post['position'],
            'pay'         =>$post['pay'],
            'nature'      =>$post['nature'],
            'province'    =>$post['province'],
            'city'        =>$post['city'],
            'district'    =>$post['district'],
            'updatetime'  =>time(),
          ];
          $expect_update = Db::name('expect')->where(['user_id'=>$post['user_id']])->update($data);
          if($expect_update){
            $arr = [
              'state'=>1,
              'message'=>'请求成功'
            ];
          }else{
            $arr = [
              'state'=>0,
              'message'=>'请求失败'
            ];
          }
          return json($arr);
      }
      $data = [
        'user_id'     =>$post['user_id'],
        'position'    =>$post['position'],
        'pay'         =>$post['pay'],
        'nature'      =>$post['nature'],
        'province'    =>$post['province'],
        'city'        =>$post['city'],
        'district'    =>$post['district'],
        'addtime'  =>time(),
      ];
      $expect_add = Db::name('expect')->insert($data);
          if($expect_add){
            $arr = [
              'state'=>1,
              'message'=>'请求成功'
            ];
          }else{
            $arr = [
              'state'=>0,
              'message'=>'请求失败'
            ];
          }
          return json($arr);
    }
    /****
     Class register           个人简历——工作经历
    */
    public function undergo(){
      $post = input('post.');
      $occupational_history = Db::name('occupational_history')->where(['user_id'=>$post['user_id'],'id'=>$post['undergo_id']])->find();
      if($occupational_history){
         $arr = [
            'state'                 =>1,
            'occupational_history'  =>$occupational_history,
            'message'               =>'请求成功'
          ];
      }else{
         $arr = [
              'state'=>0,
              'message'=>'请求失败'
            ];
      }
      return json($arr);
    }
    /****
     Class register           个人简历——工作经历添加
     *@user_id[int]           用户id
     *@undergo                工作经历
     *@position               职位名称
     *@content                工作描述
    */
    public function undergo_add(){
        $post = input('post.');
        $data = [
          'user_id'         =>$post['user_id'],
          'undergo'         =>$post['undergo'],
          'position'        =>$post['position'],
          'entrytime'       =>$post['entrytime'],
          'departuretime'   =>$post['departuretime'],
          'content'         =>$post['content'],
          'addtime'         =>time(),
        ];
        $occupational_history = Db::name('occupational_history')->insert($data);
        if($occupational_history){
          $arr = [
            'state'    =>1,
            'message'  =>'请求成功'
          ];
        }else{
         $arr = [
            'state'   =>0,
            'message' =>'请求失败'
          ];
      }
      return json($arr);
    }

    /****
     Class register           个人简历——工作经历修改
     *@user_id[int]           用户id
     *@undergo                工作经历
     *@position               职位名称
     *@content                工作描述
    */
    public function undergo_update(){
        $post = input('post.');
        $data = [
          'user_id'           =>$post['user_id'],
          'undergo'           =>$post['undergo'],
          'position'          =>$post['position'],
          'entrytime'         =>$post['entrytime'],
          'departuretime'     =>$post['departuretime'],
          'content'           =>$post['content'],
          'updatetime'         =>time(),
        ];
        $occupational_history = Db::name('occupational_history')
                              ->where(['user_id'=>$post['user_id'],'id'=>$post['undergo_id']])
                              ->update($data);
        if($occupational_history){
           $arr = [
              'state'    =>1,
              'message'  =>'请求成功'
            ];
        }else{
           $arr = [
                'state'   =>0,
                'message' =>'请求失败'
              ];
        }
        return json($arr);
    }
  
}